import * as Koa from 'koa';
import { Transaction } from 'tiny-entity2';
import { Guid, route, RouteOptionsVerify, ServiceManager, UsingLock } from "../../common";
import { ServiceException } from '../../common/excpetion';
import { ApiDataContext } from '../../domain/apiDataContext';
import { AccountPermits } from '../../domain/models/accountPermits';
import { Role } from '../../domain/models/role';
import { AccountPermitsService } from '../../service/accountPermitsService';
import { RoleService } from '../../service/RoleService';
const roleSvr = ServiceManager.Current.GetService<RoleService>('RoleService');
const accountPermitsSvr = ServiceManager.Current.GetService<AccountPermitsService>('AccountPermitsService');
class RoleController {
    @route(new RouteOptionsVerify('GET', '/api/role'))
    async queryRoles(ctx: Koa.Context, next) {
        let params=ctx.query;
        ctx.response.body = await roleSvr.QueryRole(params);
        await next();
    }
    @route(new RouteOptionsVerify('GET', '/api/role/:id'))
    async getRoleInfoById(ctx: Koa.Context, next) {
        let id=ctx.params.id;
        ctx.response.body = await roleSvr.GetRoleById(id);
        await next();
    }
    @route(new RouteOptionsVerify('POST', '/api/role'))
    async createRole(ctx: Koa.Context, next) {
        let rootOrgId = ctx.headers['orgid'];
        let role:Role = ctx.request.body;
       // role.organizationId=rootOrgId;
        checkData(role);

        role.id = Guid.GetGuid();
        role.isDelete=false;
        
        let userResult = await roleSvr.CreateRole(role);
        ctx.response.body =userResult;
        await next();
    }
    @route(new RouteOptionsVerify('PUT', '/api/role'))
    async updateRole(ctx: Koa.Context, next) {
        let role=<Role>ctx.request.body;
        let rootOrgId = ctx.headers['orgid'];
        if(!role.id)
        {
            throw new ServiceException('id 不能为空！', 'not_be_null');
        }
        //role.organizationId=rootOrgId;
        checkData(role);

        await UsingLock(role.id, async () => {
            let roledata = await roleSvr.GetRoleById(role.id);
            roledata.name=role.name?role.name:roledata.name;
            roledata.description=role.description?role.description:roledata.description;
            roledata.isGlobal=role.isGlobal?role.isGlobal:roledata.isGlobal;
            roledata.createTime=role.createTime?role.createTime:roledata.createTime;
            roledata.organizationId=role.organizationId?role.organizationId:roledata.organizationId;
            roledata.isDelete=role.isDelete?role.isDelete:roledata.isDelete;
            roledata.status=role.status?role.status:roledata.status;

            ctx.response.body = await roleSvr.UpdateRole(role);
        })

        await next();
    }
    @route(new RouteOptionsVerify('DELETE', '/api/role/:id'))
    async deleteRole(ctx: Koa.Context, next) {
        let id = ctx.params.id;
        let role =await roleSvr.GetRoleById(id);
        role.isDelete=true;

        await UsingLock(role.id, async () => {
            // await Transaction(new ApiDataContext(), async apictx => {
            //     let accountPermits=<AccountPermits>{authObjectId:id,authObjectType:"role"};
            //     accountPermitsSvr.DeleteAccountPermits(accountPermits,apictx);
                ctx.response.body = roleSvr.UpdateRole(role);
            //});
        })
       
        await next();
    }
}

const checkData = (data:Role) => {
    if (!data.name) {
        throw new ServiceException('未填写角色名称！');
    }
    if (!data.organizationId) {
        throw new ServiceException('未传入根组织编号！');
    }
}